package com.cbd.dao.impl;

import java.math.BigInteger;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cbd.dao.CompanyDao;
import com.cbd.pojo.Company;
import com.cbd.pojo.PageBean;
import com.cbd.pojo.Renter;
import com.cbd.pojo.User;
import com.cbd.util.IQuantity;
@Repository
public class CompanyDaoImpl implements CompanyDao {
	
	@Resource(name="sessionFactory")
	private SessionFactory sessionFactory;
	@Override
	public int login(User user) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateCompany(Company company) {
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createSQLQuery("UPDATE company c LEFT JOIN `user` u ON c.userID=u.userID SET c.companyBoss=?,"
				+ "c.companyTel=?,u.userName=?,u.userPWD=? WHERE c.companyID="+company.getCompanyID());
		query.setString(0, company.getCompanyBoss());
		query.setString(1, company.getCompanyTel());
		query.setString(2, company.getUser().getUserName());
		query.setString(3, company.getUser().getUserPWD());
		int result = query.executeUpdate();
		return result;
	}
	

	@Override
	public int deleteCompany(int renterId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Company> findAllCompany() {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		return session.createSQLQuery("select * from company where type = 0").list();
	}

	@Override
	public Company findCompanyById(int companyId) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Company c where c.companyID = ? and c.type= 0");
		query.setString(0, ""+companyId);
		List<Company> companys = query.list();
		Company resCompany = companys.get(0);
		resCompany.setCbdpact(null);
		resCompany.setCompanypacct(null);
		return resCompany;
	}

	@Override
	public PageBean findAllUserComppany(int page,int zhi,String name) {
		// TODO Auto-generated method stub
		//System.out.println(page+" "+zhi+" "+name);
		Session session = sessionFactory.getCurrentSession();
		String sql="SELECT c.companyID,c.companyName,c.companyAddress,c.companyBoss,c.companyTel FROM company c ";
		if(zhi==1){
			sql+=" WHERE c.type='0' and c.companyName LIKE '%"+name.trim()+"%'LIMIT ?,?";
		}else if(zhi==2){
			sql+="WHERE  c.type='0' and c.companyBoss LIKE '%"+name.trim()+"%' LIMIT ?,?";
		}else{
			sql+="WHERE  c.type='0' and c.companyTel LIKE '%"+name.trim()+"%' LIMIT ?,?";
		}
		
		Query query=session.createSQLQuery(sql);
		
		if(page<=1){
			page=1;
		}
		query.setParameter(0, (page-1)*IQuantity.pageSize);
		query.setParameter(1, IQuantity.pageSize);
		List list= query.list();
		
		String sql2=("SELECT count(*) FROM company c ");
			if(zhi==1){
				sql2+=" WHERE  c.type='0' and c.companyName LIKE '%"+name.trim()+"%'";
			}else if(zhi==2){
				sql2+="WHERE  c.type='0' and c.companyBoss LIKE '%"+name.trim()+"%'";
			}else{
				sql2+="WHERE  c.type='0' and c.companyTel LIKE '%"+name.trim()+"%'";
			}
		Query query2=session.createSQLQuery(sql2);
		BigInteger count=(BigInteger) query2.list().get(0);
		String cou=""+count;
		
		PageBean bean=new PageBean();
		bean.setList(list);
		int pageSize=0;
		int count1=Integer.parseInt(cou);
		//System.out.println("count"+count1);
		
			pageSize=count1%IQuantity.pageSize==0?count1/IQuantity.pageSize:(int)(count1/IQuantity.pageSize)+1;
		
		
		bean.setPageSize(pageSize);
		return bean;
	}

	@Override
	public int delectEnterprise(int id) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		//System.out.println(" dao"+id);
		Query query=session.createSQLQuery("update company set type=? where companyID=?");
		query.setParameter(0, 1);
		query.setParameter(1, id);
		int cou= query.executeUpdate();
		return cou;
	}

	@Override
	public void addCompany(Company company) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		session.save(company);
		
	}

	@Override
	public Company findCompanyByUserId(int userId) {
		// TODO Auto-generated method stub
		Session session = sessionFactory.getCurrentSession();
		Query query = session.createQuery("from Company c where c.user.userID = ?");
		query.setParameter(0, userId);
		return (Company) query.list().get(0);
	}

}
